function varargout = MEMC_ImageInfo(varargin)
% MEMC_IMAGEINFO M-file for MEMC_ImageInfo.fig
%      MEMC_IMAGEINFO, by itself, creates a new MEMC_IMAGEINFO or raises the existing
%      singleton*.
%
%      H = MEMC_IMAGEINFO returns the handle to a new MEMC_IMAGEINFO or the handle to
%      the existing singleton*.
%
%      MEMC_IMAGEINFO('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in MEMC_IMAGEINFO.M with the given input arguments.
%
%      MEMC_IMAGEINFO('Property','Value',...) creates a new MEMC_IMAGEINFO or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before MEMC_ImageInfo_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to MEMC_ImageInfo_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help MEMC_ImageInfo

% Last Modified by GUIDE v2.5 26-Jul-2010 16:48:16

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @MEMC_ImageInfo_OpeningFcn, ...
                   'gui_OutputFcn',  @MEMC_ImageInfo_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before MEMC_ImageInfo is made visible.
function MEMC_ImageInfo_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to MEMC_ImageInfo (see VARARGIN)

if nargin && isequal(varargin{1},'MEMC_ImageTypeList')
   handles.list  = varargin{2};
   handles.scale = varargin{3};
   handles.blend = varargin{4};
end

handles.scale_prev = handles.scale;
handles.blend_prev = handles.blend;

handles.ShownList = {};
for k=1:length(handles.list)
    handles.ShownList = {handles.ShownList{:},GetShownListString(handles,k)};
end

set(handles.tagTypeList,'String',handles.ShownList);

% Choose default command line output for MEMC_ImageInfo

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes MEMC_ImageInfo wait for user response (see UIRESUME)
uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = MEMC_ImageInfo_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.scale;
varargout{2} = handles.blend;
delete(hObject);

% --- Executes on selection change in tagTypeList.
function tagTypeList_Callback(hObject, eventdata, handles)
% hObject    handle to tagTypeList (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns tagTypeList contents as cell array
%        contents{get(hObject,'Value')} returns selected item from tagTypeList
if isfield(handles,'holdon')
   if handles.holdon == 1
      return;
   end
end
handles.holdon = 1;
guidata(hObject, handles);

Selected = get(hObject,'Value');

tmpStr = sprintf('%s',handles.list{Selected}{1});
for k = 2:length(handles.list{Selected})
    tmpStr = sprintf('%s.%s',tmpStr,handles.list{Selected}{k});
end

nodeSizeType = length(handles.scale{Selected});
if length(handles.list)<2 && nodeSizeType == 0
    handles.holdon = 0;
    guidata(hObject, handles);
    return;
end

bBlendEnable = 1;
[handles.scale{Selected},handles.blend{Selected}] = MEMC_MATScaleWnd('TypeName',tmpStr,nodeSizeType,...
                                                    handles.scale{Selected},handles.blend{Selected},bBlendEnable);
handles.ShownList{Selected} =  GetShownListString(handles,Selected);
set(handles.tagTypeList,'String',handles.ShownList);

handles.holdon = 0;
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function tagTypeList_CreateFcn(hObject, eventdata, handles)
% hObject    handle to tagTypeList (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in tagOKButton.
function tagOKButton_Callback(hObject, eventdata, handles)
% hObject    handle to tagOKButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
uiresume(handles.figure1);


% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: delete(hObject) closes the figure
hwndList = findobj('Name','MEMC_MATScaleWnd');
if ~isempty(hwndList);
  return;
end
handles.scale = handles.scale_prev;
handles.blend = handles.blend_prev;
guidata(hObject, handles);
uiresume(handles.figure1);



function ListString = GetShownListString(handles,idx)
ListString = sprintf('%s',handles.list{idx}{1});
for k = 2:length(handles.list{idx})
     ListString = sprintf('%s.%s',ListString,handles.list{idx}{k});
end
len = 40-length(ListString);
if len<1
   len = 1;
end
space = zeros(1,len);
space(:) = ' ';
ListString = sprintf('%s%s',ListString,char(space));
%scale
scaleLen = length(handles.scale{idx});
if  scaleLen == 1
    ListString = sprintf('%s scale:[Scald]=[%g]',ListString,handles.scale{idx}(1));
elseif scaleLen == 2
    ListString = sprintf('%s scale:[Scaly,Scaluv]=[%g,%g]',ListString,handles.scale{idx}(1),handles.scale{idx}(2));
end
%blend
if ~isempty(handles.list)
   len = 70-length(ListString);
   if len<1
      len = 1;
   end
   space = zeros(1,len);
   space(:) = ' ';
   ListString = sprintf('%s%s',ListString,char(space));
   ListString = sprintf('%sblend:[%g,%g,%g]',ListString,handles.blend{idx}(1),...
                                handles.blend{idx}(2),handles.blend{idx}(3));
end



